<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Mail;
use DB;
use Crypt;

class ResetController extends Controller
{
    //forget
    public function forget()
    {
    	return view('admin.reset.email');
    } 

    // send
    public function send(Request $request)
    {
    	// 开始发送邮件
    	// Mail::raw('这是邮件的内容', function($message){
    	// 	$message -> to('1364998490@qq.com');
    	// 	$message -> subject('这是邮件的主题');
    	// });

    	// // 发送模板邮件
    	//  Mail::send('email.forget', [], function($message){
    	//  		$message -> to('1364998490@qq.com');
    	// 		$message -> subject('这是邮件的主题');
    	//  });


    	$email = $request -> input('email');
        $arr = explode('@', $email);

        $tiaoEmail = 'http://mail.'.$arr[1];

    	$res = DB::table('users') -> where('email', $email) -> first();
    	if(!$res)
    	{
    		return back() -> with(['info' => '邮箱不存在']);
    	}

    	Mail::send('admin.email.forget', ['res' => $res ], function($message) use ($email){
    			$message -> to($email);
    			$message -> subject('这是邮件的主题');
    	});
        return view('admin.reset.tiao', ['tiaoEmail' => $tiaoEmail]);

    }
    public function find($token)
    {
    	$res = DB::table('users') -> where("token", $token) -> first();
        if(!$res)
        {
            return redirect('/admin/reset/info') -> with(['info' => '这是一个不合法的来源, 请走开']);
        }

        return view('admin.reset.newpass', ['res' =>$res]);
    }

    public function info()
    {
        return view('admin.reset.info');
    }

    public function uppass(Request $request)
    {
        $data = $request -> except('_token');

        $password = Crypt::encryptString($data['password']);

        $res = DB::table('users') -> where('email', $data['email']) -> update(['password' => $password]);
        if($res)
        {
            return redirect('/admin/login') -> with(['info' => '修改成功']);
        }else
        {
            return back() -> with(['info' => '修改失败']);
        }
    }
}
